package com.qf.servlet;

import com.qf.dao.C_vDao;
import com.qf.entity.C_v;
import com.qf.service.C_vService;
import com.qf.service.impl.C_vServiceImpl;
import com.qf.utils.JsonUtils;
import com.qf.utils.PageUtils;
import com.qf.utils.ResultVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(name = "c_vServlet", urlPatterns = "/c_vServlet")
public class C_vServlet extends BaseServlet {
    private C_vService c_vService = new C_vServiceImpl();

    /**
     * 批量发放
     *
     * @param req
     * @param resp
     * @throws ParseException
     * @throws SQLException
     * @throws IOException
     */
    public void addC_v(HttpServletRequest req, HttpServletResponse resp) throws ParseException, SQLException, IOException {
        //获取输出流对象
        PrintWriter pw = resp.getWriter();
        //实例化结果对象
        ResultVo<Boolean> resultVo = new ResultVo<>();
        //获取前端传来的参数
        Integer cid = null;
        String cidStr = req.getParameter("cid");
        //当传来的参数不是空或者空串的时候
        if (cidStr != null && !"".equals(cidStr)) {
            cid = Integer.parseInt(cidStr);
        }
        Date startTime = null;
        Date endTime = null;
        String startTimeStr = req.getParameter("startTime");
        String endTimeStr = req.getParameter("endTime");
        if (startTimeStr != null && !"".equals(startTimeStr)) {
            startTime = new SimpleDateFormat("yyyy-MM-dd").parse(startTimeStr);
        }
        if (endTimeStr != null && !"".equals(endTimeStr)) {
            endTime = new SimpleDateFormat("yyyy-MM-dd").parse(endTimeStr);
        }
        Date updateTime = new Date();
        String vids = req.getParameter("vids");
        if (vids != null && !"".equals(vids)) {
            //当存在值,把值拆分了
            String[] vidArray = vids.split(",");
            C_v[] c_vs = new C_v[vidArray.length];
            //循环给c_vs赋值
            for (int i = 0; i < vidArray.length; i++) {
                if (vidArray[i] != null && !"".equals(vidArray[i])) {
                    int vid = Integer.parseInt(vidArray[i]);
                    C_v c_v = new C_v();
                    c_v.setC_v_vid(vid);
                    c_v.setC_v_cid(cid);
                    c_v.setC_v_update_time(updateTime);
                    c_v.setC_v_startTime(startTime);
                    c_v.setC_v_endTime(endTime);
                    c_vs[i] = c_v;
                }
            }
            //调用Service层方法
            int num = c_vService.addC_v(c_vs);
            if (num > 0) {
                resultVo.setCode(200);
                resultVo.setData(true);
                resultVo.setMessage("增加成功");
            } else {
                resultVo.setCode(500);
                resultVo.setData(false);
                resultVo.setMessage("增加失败");
            }
        } else {
            //添加的为null值
            C_v c_v = new C_v();
            c_v.setC_v_cid(cid);
            c_v.setC_v_vid(null);
            c_v.setC_v_startTime(startTime);
            c_v.setC_v_endTime(endTime);
            c_v.setC_v_update_time(updateTime);
            //调用service层方法
            C_v[] c_vs = {c_v};
            int num = c_vService.addC_v(c_vs);
            if (num > 0) {
                resultVo.setCode(200);
                resultVo.setData(true);
                resultVo.setMessage("增加成功");
            } else {
                resultVo.setCode(500);
                resultVo.setData(false);
                resultVo.setMessage("增加失败");
            }
        }
        JsonUtils.showJson(pw, resultVo);
    }

    /**
     * 分页展示发放记录的功能
     *
     * @param req
     * @param resp
     * @throws IOException
     * @throws SQLException
     */
    public void showC_vByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        //获取输出流对象
        PrintWriter pw = resp.getWriter();
        //实例化结果对象
        ResultVo<PageUtils<C_v>> resultVo = new ResultVo<>();
        //获取前端传来的参数
        String currentPageNo = req.getParameter("currentPageNo");
        String pageSize = req.getParameter("pageSize");
        //调用service层方法
        PageUtils<C_v> pageUtils = c_vService.getC_vPageUtils(currentPageNo, pageSize);
        if (pageUtils != null) {
            resultVo.setCode(200);
            resultVo.setMessage("显示成功");
            resultVo.setData(pageUtils);
        } else {
            resultVo.setCode(500);
            resultVo.setMessage("显示失败");
            resultVo.setData(null);
        }
        JsonUtils.showJson(pw, resultVo);
    }


}
